#!/bin/sh
# generateASMCtrlFileScript.sh
# takes mounted directory where image copy resides and generates datafile clause for clone controlfile creation
set +x

SID=$1
ASMDISK=$2
OSID=$3
guser=$4

psid=$OSID

scriptsRoot=${LB_ROOT}
if [ -z "$scriptsRoot" ];then
    echo "missing env LB_ROOT"
    exit 1
fi

nsidlen=`echo -n $SID | wc -m`
if [ $nsidlen -gt 8 ]; then
newdb_name=`echo $SID | cut -c1-8`
else
newdb_name=$SID
fi

DATAFILES=""
CLONE_CRT_SQL="Clone_createCtrlFileASM_"$SID".sql"
if [ -e $CLONE_CRT_SQL ]; then
   echo "removing $CLONE_CRT_SQL before generating a new one"
   rm -f $CLONE_CRT_SQL
fi
ASMDISK1="+"$ASMDISK

if [ ! -z "$redosize" ]; then
rsize=$redosize"M"
else
rsize=500"M"
fi

if [ ! -z "$characterset" ]; then
charset=`echo $characterset | tr '[a-z]' '[A-Z]'`
else
charset="AL32UTF8"
fi

echo  "SET FEEDBACK 1\nSET NUMWIDTH 10\nSET LINESIZE 80\nSET TRIMSPOOL ON\nSET TAB OFF\nSET PAGESIZE 100\n" >> $CLONE_CRT_SQL
echo  "CREATE CONTROLFILE SET DATABASE $newdb_name RESETLOGS" >> $CLONE_CRT_SQL
echo  "MAXLOGFILES 64\nMAXLOGMEMBERS 4\nMAXDATAFILES 10000\nMAXINSTANCES 1\nMAXLOGHISTORY 800" >> $CLONE_CRT_SQL
echo  "LOGFILE\nGROUP 1 '$ASMDISK1/$SID/onlinelog/redo1.log' SIZE $rsize,\nGROUP 2 '$ASMDISK1/$SID/onlinelog/redo2.log' SIZE $rsize,\nGROUP 3 '$ASMDISK1/$SID/onlinelog/redo3.log' SIZE $rsize,\nGROUP 4 '$ASMDISK1/$SID/onlinelog/redo4.log' SIZE $rsize" >> $CLONE_CRT_SQL
echo  "DATAFILE" >> $CLONE_CRT_SQL

var="+ASM"
for ASMSID in $var; do
  i=`echo $ASMSID | tr '[a-z]' '[A-Z]'`
ASMS=`grep -v "^[#]" /etc/oratab | grep "$ASMSID" | head -n1 | cut -f1 -d":"`
 echo "asmsid is: $ASMS"
done

var="$ASMS"
for ASM_SID in $var; do
  i=`echo $ASM_SID | tr '[a-z]' '[A-Z]'`
ASMHOME=`grep -v "^[#]" /etc/oratab | grep "$ASM_SID" | head -n1 | cut -f2 -d":"`
 echo "asmhome is: $ASMHOME"
done

export ORACLE_BASE=$ASM_BASE
export ORACLE_HOME=$ASMHOME
export LD_LIBRARY_PATH=$ASMHOME/lib:/usr/lib:/lib
export PATH=$ASMHOME/bin:$PATH
export ORACLE_SID="$ASMS"
asmcmd=$ASMHOME/bin/asmcmd

dbsids=`su $guser -c "asmcmd ls +$ASMDISK" | awk -F"/" '{print $1}'`
for d in ${dbsids}
do
FILE_LISTN=`su $guser -c "$asmcmd ls -l +$ASMDISK/$d/datafile | grep -i datafile | grep + | rev | cut -d\" \" -f1 | rev"`
for f in ${FILE_LISTN}
 do
   if [ -z "$DATAFILES" ]; then
        #DATAFILES=${DATAFILES}"'"+$ASMDISK/$d/datafile/${f}"'"
	DATAFILES=${DATAFILES}"'"${f}"'"
   else
        found=`echo "$DATAFILES" | grep -i "$f"`
        if [ -z "$found" ]; then
                #DATAFILES=${DATAFILES}",\n'"+$ASMDISK/$d/datafile/${f}"'"
                DATAFILES=${DATAFILES}",\n'"${f}"'"
        fi
   fi
 done

FILE_LIST=`su $guser -c "$asmcmd ls -l +$ASMDISK/$d/datafile | grep -i datafile | rev | cut -d\" \" -f1 | rev | grep -v +"`
for f in ${FILE_LIST}
 do
   if [ -z "$DATAFILES" ]; then
        DATAFILES=${DATAFILES}"'"+$ASMDISK/$d/datafile/${f}"'"
   else
	found=`echo "$DATAFILES" | grep -i "$f"`
       	if [ -z "$found" ]; then
        	DATAFILES=${DATAFILES}",\n'"+$ASMDISK/$d/datafile/${f}"'"
        fi
   fi
 done
done

echo  "$DATAFILES" >> $CLONE_CRT_SQL
echo  "CHARACTER SET $charset;\n" >> $CLONE_CRT_SQL

osuser=$username
if [ -z "$osuser" ]; then
   chmod 755 $CLONE_CRT_SQL
else
   . ${scriptsRoot}/oracle/oracleclone/OracleAppMountComFunc.sh
   change_own $osuser $CLONE_CRT_SQL
fi

export ORACLE_BASE=
exit 0

